<?php
class Profile_IndexController extends Zendvn_Controller_Action{
	
	public  function init(){
		
		//check login cookie
		
		$user = new Registration_Model_Auth();
		
		if($user->checkLoginCookieToken() == false){
			
			//$this->_redirect("/registration/auth/login/");
			
		}
		
		$this->createTemplate(APPLICATION_PATH . '/templates/dashboard','template.ini','template');
	}
	
	public function indexAction(){
		
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
		
		$user_model = new Registration_Model_Auth();
		
		$this->view->user_profile = $user_model->getItemById($data->id);
		
		$user = Cu_Facebook::getUser();
			
		
		if ($user) {
			
		     try {
		     	
		     	$access_token = Cu_Facebook::getAccessToken();
		     	
        		$accounts = Cu_Facebook::api('/me/accounts');
        						    
        		$this->view->accounts = $accounts; 
				
        		
		    } catch (FacebookApiException $e) {
		        echo $e->getMessage();
		    }
		}
		
		if($this->getRequest()->isPost()){
			
			$displayname = strip_tags(@$_POST["displayname"]);
			$email = strip_tags(@$_POST["email"]);
			$password = @$_POST["password"];			
			$phone1 = @$_POST["phone1"];
			$phone2 = @$_POST["phone2"];
			$fanpage = @$_POST["fanpage"];
			$address1 = @$_POST["address1"];
			$address2 = @$_POST["address2"];
			
			$this->view->errorMessage = "";
			
			$flag = true;
			
			if($displayname == ""){
                $this->view->errorMessage = "Tên không được để trống.";                	
                $flag = false;                
            }
            elseif($email == ""){
                $this->view->errorMessage = "Email không được để trống.";
                $flag = false;               
            }
            elseif($password == ""){
                $this->view->errorMessage = "Password không được để trống.";
                $flag = false;                
            }
            elseif($phone1 == ""){
                $this->view->errorMessage = "Số điện thoại không được để trống.";
                $flag = false;             
            }                
			elseif($address1 == ""){
                $this->view->errorMessage = "Địa chỉ không được để trống.";
                $flag = false;                
            }              
            elseif(filter_var($email, FILTER_VALIDATE_EMAIL) == False) {
                $this->view->errorMessage = "Email không tồn tại.";
                $flag = false;                
            }

            if($flag){
                $pageid ="";
                $pagename = "";
                
				if($fanpage != ""){
                	$str_pageid = explode("|", $fanpage);
                	$pageid = $str_pageid[0];
                	$pagename = str_replace($pageid."|","",$fanpage); 
                }
                
                $password = hash('sha256',"-".'!@#thietdep'."-".$_POST['password']."-"); 
                
                //update user
                
                $user = new Registration_Model_Auth();
                
                $arr = array(
                	"id"=>$data->id,
                	"displayname"=>$displayname,
                	"email"=>$email,
                	"password"=>$password,
                	"phone"=>$phone1,
                	"address"=>$address1,
                );
                
                $user->updateProfile($arr);
                
                //create shop                
                $shop = new Shop_Model_Index();
                
                if($pageid != ""){
                	
	                if($shop_item = $shop->getItemByPageId($pageid)){
	                	
	                	$arr = array(		      
	                		"id"=>$shop_item["id"],         	                	
		                	"phone1"=>$phone1,
		                	"phone2"=>$phone2,                	
		                	"address1"=>$address1,
		                	"address2"=>$address2,	                		
	                	);
	                	$shop->updateShop($arr);	
	                }else{	                	
	                	$arr = array(
		                	"name"=>$pagename,
		                	"pageid"=>$pageid,
		                	"userid"=>$data->id,                	
		                	"phone1"=>$phone1,
		                	"phone2"=>$phone2,                	
		                	"address1"=>$address1,
		                	"address2"=>$address2,
	                	);
	                	$shop->insertShop($arr);
	                }

	                $this->_redirect("/app");
	                
                }
                
            }
		}
		
	}
}